package com.lczyfz.wisdom.modules.platform.wxutils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lczyfz.wisdom.modules.platform.constant.Constant;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

/**
 * Create by css on 2019/9/29
 */
public class WeiXinUtils {

    public static String getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    public static String GET_USER_INFO = "https://api.weixin.qq.com/cgi-bin/user/info";

    public static String GET_ALL_USER_OPENID = "https://api.weixin.qq.com/cgi-bin/user/get";


    public static String allUserInfo(){
        WeiXinUtils weiXinUtils = new WeiXinUtils();
        String token = weiXinUtils.getWxToken(GET_ALL_USER_OPENID);
        String url = GET_ALL_USER_OPENID + "?access_token=" + token + "&next_openid=";
        String connection = getConnection(url);
        try {
            String response = new String(connection.getBytes("ISO-8859-1"),"UTF-8");
            return response;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "11";
    }

    /**
     * 获取微信用户基本信息
     * @return
     */

    public static String getWxUserInfo(String openid){
        //String openid = "osi95v-5EI-_YiDv-tKs9ttlXdOY";
        String token = getWxToken(GET_USER_INFO);
        String s = GET_USER_INFO + "?access_token=" + token + "&openid=" + openid;
        String connection = getConnection(s);
        try {
            String response = new String(connection.getBytes("ISO-8859-1"),"UTF-8");
            return response;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "11";
    }

    /**
     * 获取请求结果
     * @param s
     * @return
     */
    public static String getConnection(String s){
        InputStream inputStream = null;
        HttpURLConnection connection = null;
        String result = "";
        try {
            connection= (HttpURLConnection) new URL(s).openConnection();
            connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
            connection.setConnectTimeout(300000);
            connection.setRequestMethod("GET");
            connection.setReadTimeout(300000);
            connection.connect();
            inputStream = connection.getInputStream();
            int len = 0;
            while ((len = inputStream.read()) != -1){
                result += (char)len;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                connection.disconnect();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 获取微信token
     * @param tokenUrl
     * @return
     */
    public static String getWxToken(String tokenUrl){
        Properties properties = Constant.getProperties();
        Object appID = properties.get("appID");
        String secret = properties.getProperty("appsecret");
        tokenUrl = getToken + "&appid=" + appID + "&secret=" + secret;
        String info = getConnection(tokenUrl);
        JSONObject jsonObject = JSON.parseObject(info);
        return  (String) jsonObject.get("access_token");
    }











}
